﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.Web.UI.Controls;
using ZX.SYS.Library;
using ZX.CRM.Bussiness;
using ZX.CRM.Library;
using ZX.DBModule;
using ZX.Common;
using ZX.BLL.Extend;
using ZX.BLL;
using ZX.Mapping;
using ZX.Web.UI.Controls.ControlBind;
using System.Data;
using ZX.CRM.Web.CrmWorkFlowService;

namespace ZX.WebApp.Crm.Deliver
{
    public partial class AddDeliverHr : EditUIPage
    {
        BTCrmCrustomerOrder order = new BTCrmCrustomerOrder();
        BTCrmCustomer bll = new BTCrmCustomer();
        BDatPhraseDetail bllDetail = new BDatPhraseDetail();
        BCrmDeliverMajor bllDeliverMajorProject = new BCrmDeliverMajor();
        WorkFlowServiceClient client = new WorkFlowServiceClient();
        string strListDetailHouseType = "[]",
            strListDetailTrafficType = "[]",
            strDeliverMajorInfo = "[]", strMinZu = "";
        string strDeliverTrainInfo = "[]", wfid = "", orderProduct = "", wtid = "", strTeacher = "", orderId = "", productId = "";
        /// <summary>
        /// 退回标志
        /// </summary>
        protected string strBack { get; set; }
        public string workflowInsId
        {
            get;
            set;
        }
        int strAllMoney;

        public int StrAllMoney
        {
            get { return strAllMoney; }
            set { strAllMoney = value; }
        }
        BCrmDeliverTrain OrderProduct = new BCrmDeliverTrain();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["WorkflowInsId"] != null)
            {
                workflowInsId = Request["WorkflowInsId"].ToString();
                Inits(workflowInsId);
            }
            strAllMoney = bll.GetPrivateNum("IsAllMoney");//1.表示需要完全款才能提交 其他则相反
            string strEntity = this.GetStringFromQueryString("entity", "E_Menu");
            string ids = this.GetStringFromQueryString("id", "");
            BaseEntity instance = ZX.Common.Globals.getEntity(strEntity) as BaseEntity;
            BaseBussnies bInstance = new BLL.Extend.BaseBussnies(instance);
            BaseEntity entity = bInstance.GetEntityById(ids) as BaseEntity;
            this.EditTemplate.Text = CreateInstanceJsObject(instance, entity);
            this.ID.Value = "";
            #region ------------ jsonData ------------
            //房型
            IList<DatPhraseDetail> ListDetailHouseType = bllDetail.GetDatPhraseDetailByClassValue("HouseType");
            if (ListDetailHouseType != null && ListDetailHouseType.Count > 0)
            {
                IList<BaseEntity> _listHouseType = new List<BaseEntity>();
                foreach (var _e in ListDetailHouseType)
                {
                    _listHouseType.Add(_e);
                }
                strListDetailHouseType = _listHouseType.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
            }
            //交通方式
            IList<DatPhraseDetail> ListDetailTrafficType = bllDetail.GetDatPhraseDetailByClassValue("TrafficType");
            if (ListDetailTrafficType != null && ListDetailTrafficType.Count > 0)
            {
                IList<BaseEntity> _listTrafficType = new List<BaseEntity>();
                foreach (var _e in ListDetailTrafficType)
                {
                    _listTrafficType.Add(_e);
                }
                strListDetailTrafficType = _listTrafficType.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
            }
            //民族
            IList<DatPhraseDetail> ListMinZu = bllDetail.GetDatPhraseDetailByClassValue("MinZu");
            if (ListMinZu != null && ListMinZu.Count > 0)
            {
                IList<BaseEntity> _ListMinZu = new List<BaseEntity>();
                foreach (var _e in ListMinZu)
                {
                    _ListMinZu.Add(_e);
                }
                strMinZu = _ListMinZu.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
            }
            #endregion
            //交付单状态
            IList<DatPhraseDetail> ListDliverId = bllDetail.GetDatPhraseDetailByClassValue("DeliverState");
            DropDownListControl.DropDataSourceBind(this.DliverState, ListDliverId, "PhraseText", "PhraseID");
            if (Flag == "0")
            {
                this.Code.Text = IDMaker.GetSequenceStr("HR001");
                this.DeliverDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
                this.ID.Value = Guid.NewGuid().ToString();
                DliverState.SelectedValue = "8A41F2EF-989F-4871-BBE9-91CDFF4235CA";
                this.txtXm.Value = this.User.UName;
                this.WorkFlowInsId.Value = Guid.NewGuid().ToString();
                this.WorkTaskInsId.Value = Guid.NewGuid().ToString();
            }
            if (Flag == "1")
            {

                string strId = this.GetStringFromQueryString("id", "");
                if (strId == "")
                {
                    strId = this.GetStringFromQueryString("WorkflowInsId", "");
                }
                IList<V_CrmDeliverHrInfo> instanceMajor = bllDeliverMajorProject.GetAllDeliverHR(new Guid(strId));
                if (instanceMajor != null && instanceMajor.Count > 0)
                {
                    wfid = instanceMajor[0].WorkFlowId;
                    wtid = instanceMajor[0].WorkTaskId;
                    if (strDescript == "nxja" || strDescript == "market")
                    {
                        btnMemember.Visible = false;
                        DliverState.SelectedValue = "20F825C4-292A-4386-8157-923668BD2682";
                    }
                    btnOrderProductID.Visible = false;
                    if (strView != "unDone")
                    {
                        btnClassFrontServer.Visible = false;
                        MgrTel.Disabled = true;
                        LinkManTel.Disabled = true;
                        LinkMan.Disabled = true;
                        ddlSingle.Enabled = false;
                        ProductList.Enabled = false;
                        txtEmail.Disabled = true;
                        txtExplain.Disabled = true;
                    }

                    if (strView == "check")
                    {
                        //txtExplain.Disabled = true;
                        //txtDay.Disabled = true;
                        //txtEndTime.Disabled = true;
                        //txtStartDate.Disabled = true;
                        
                        txtGangerTel.Disabled = true;
                        btnMemember.Visible = false;
                        ddlSingle.Enabled = false;
                        ProductList.Enabled = false;
                    } OrderProductID.Value = instanceMajor[0].OrderProductID.ToString();
                    orderId = instanceMajor[0].OrderID.ToString();
                    productId = instanceMajor[0].ProductId.ToString();
                    orderProduct = instanceMajor[0].OrderProductID.ToString();
                    DliverState.SelectedValue = instanceMajor[0].DliverState.ToString();
                    this.ID.Value = instanceMajor[0].ID.ToString();
                    this.Code.Text = instanceMajor[0].Code;
                    this.DeliverDate.Value = instanceMajor[0].AddTime.ToString("yyyy-MM-dd") == "0001-01-01" ? strView == "check" ? "" : DateTime.Now.ToString("yyyy-MM-dd") : instanceMajor[0].AddTime.ToString("yyyy-MM-dd");
                    this.txtOrderProductID.Value = instanceMajor[0].OrderCode;
                    this.CustomerCode.Value = instanceMajor[0].Code;
                    this.CustomerName.Value = instanceMajor[0].CustomerName;
                    this.Account.Value = instanceMajor[0].Amount.ToString();
                    this.Term.Value = instanceMajor[0].TermId.ToString();
                    this.txtTerm.Value = instanceMajor[0].TermName;
                    this.txtHaveNum.Value = (instanceMajor[0].TermNum-instanceMajor[0].TermExistNum).ToString();
                    this.txtBeginDate.Value = instanceMajor[0].StartTime.ToShortDateString();
                    this.txtEndDate.Value = instanceMajor[0].EndTime.ToShortDateString();
                    this.MgrTel.Value = instanceMajor[0].MgrTel;   //客户经理电话
                    this.LinkMan.Value = instanceMajor[0].ConsultPerson; //接洽人
                    this.LinkManTel.Value = instanceMajor[0].ConsultPersonTel; //接洽人电话
                    this.txtClassNo.Value = instanceMajor[0].ClassNo;//班号
                    this.txtEmail.Value = instanceMajor[0].Email;//公司邮箱
                    this.OrderDeptName.Value = instanceMajor[0].OrderDeptName;
                    this.Decisionmaker.Value = instanceMajor[0].Decisionmaker;
                    this.DecisionTel.Value = instanceMajor[0].DecisionTel;
                    this.Address.Value = instanceMajor[0].Address;
                    this.CustomerId.Value = instanceMajor[0].CustomerId.ToString();
                    this.Pay1.Value = instanceMajor[0].AccountReceivable.ToString();
                    this.Pay2.Value = instanceMajor[0].ResignMoney.ToString();
                    this.Pay3.Value = instanceMajor[0].Amount == 0 ? "0" : (instanceMajor[0].AccountReceivable / instanceMajor[0].Amount).ToString();
                    this.OrderTypeName.Value = instanceMajor[0].CustomerOrigin;
                    this.CompanyProduct.Value = instanceMajor[0].CompanyProduct;
                    this.TypeName.Value = instanceMajor[0].CustomerType;
                    this.AddUserName.Value = instanceMajor[0].CInfoer;
                    this.SaleaddName.Value = instanceMajor[0].CSaler;
                    this.LectorName.Value = instanceMajor[0].CTeacher;
                    this.CustomerMgrName.Value = instanceMajor[0].CManager;
                    this.txtClassFrontServer.Value = instanceMajor[0].TrainName;
                    this.txtXm.Value = instanceMajor[0].UName;
                    this.ddlSingle.SelectedValue = instanceMajor[0].IsDealEP;//是否参加过鹰计划 0，否；1，是
                    this.ProductList.SelectedValue = instanceMajor[0].ProductId.ToString();
                    this.txtStartDate.Value = instanceMajor[0].BeginDate.ToString("yyyy-MM-dd") == "0001-01-01" ? "" : instanceMajor[0].BeginDate.ToString("yyyy-MM-dd");
                    this.txtGangerTel.Value = instanceMajor[0].GangerTel;
                    this.txtExplain.Value = instanceMajor[0].Explains;
                    this.txtAddTime.Value = instanceMajor[0].ApplicantDate.ToString("yyyy-MM-dd") == "0001-01-01" ? strView == "check" ? "" : DateTime.Now.ToString("yyyy-MM-dd") : instanceMajor[0].ApplicantDate.ToString("yyyy-MM-dd");
                    if ((instanceMajor[0].ApplicantUser.ToString().Length > 0 && instanceMajor[0].ApplicantUser != Guid.Empty) || strView == "check")
                    {
                        this.AddUser.Value = instanceMajor[0].ApplicantUser.ToString(); //填写人ID
                        this.txtAddUser.Value = instanceMajor[0].AddPmName;//填写人
                    }
                    else
                    {
                        this.AddUser.Value = this.UserId.ToString();
                        this.txtAddUser.Value = this.User.UName;
                    }
              
                    this.txtMemember.Value = instanceMajor[0].GangerName;//班主任

                   
                  
                    

                    IList<BaseEntity> _listDeliverTrainInfo = new List<BaseEntity>();
                    foreach (var _e in instanceMajor)
                    {
                        _listDeliverTrainInfo.Add(_e);
                    }
                    strDeliverTrainInfo = _listDeliverTrainInfo.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
                    //交付单状态选中
                    //DropDownListControl.DropDownListSelect(this.DliverState, instanceMajor[0].DliverState.ToString());
                }
                this.WorkTaskInsId.Value = this.GetStringFromQueryString("WorktaskInsId", "");
                WorkFlowInsId.Value = this.GetStringFromQueryString("WorkflowInsId", "");

            }

        }
        /// <summary>
        /// 初始化流程数据
        /// </summary>
        /// <param name="workflowInsId"></param>
        public void Inits(string workflowInsId)
        {
            DataTable dt = client.GetWorkflowdetail(workflowInsId);
            //if (dt == null || dt.Rows.Count == 0)
            //{
            //    gvworkflowMessage.Visible = false;
            //}
            //else
            //{
            gvworkflowMessage.DataSource = dt;
            gvworkflowMessage.DataBind();
            //}
        }

        #region-----------
        public string PageTitle
        { get { return this.GetStringFromQueryString("title", "") == "" ? "交付申请单--英才学校" : this.GetStringFromQueryString("title", ""); } }
        public string strDescript
        { get { return this.GetStringFromQueryString("descript", ""); } }
        public string Teacher
        {
            get { return strTeacher; }
        }
        public string Flag
        { get { return this.GetStringFromQueryString("flag", ""); } }
        public string ListRecord
        { get { return GetListCrmProduct(); } }
        /// <summary>
        /// 弹出列表客户经理ID
        /// </summary>
        public string addUser { get { return this.UserId.ToString(); } }
        private string GetListCrmProduct()
        {
            IList<V_CrmOrderProduct> list = order.GetAllOrderProduct(new Guid(this.ID.Value));
            if (list != null && list.Count > 0)
            {
                return ZX.Common.Globals.ObjectToJSON(list);
            }
            else
            {
                return "";
            }
        }


        /// <summary>
        /// 房型
        /// </summary>
        public string ListDetailHouseType
        { get { return strListDetailHouseType; } }
        /// <summary>
        /// 交通方式
        /// </summary>
        public string ListDetailTrafficType
        { get { return strListDetailTrafficType; } }
        /// <summary>
        /// 民族
        /// </summary>
        public string ListMinZu
        { get { return strMinZu; } }
        /// <summary>
        /// 信息集合
        /// </summary>
        public string StrDeliverTrainInfo
        { get { return this.strDeliverTrainInfo; } }
        public string strWorkFlowId
        { get { return this.GetStringFromQueryString("wfid", "") == "" ? wfid : this.GetStringFromQueryString("wfid", ""); } }
        /// <summary>
        /// 
        /// </summary>
        public string strWorkTaskId
        { get { return this.GetStringFromQueryString("wtid", "") == "" ? wtid : this.GetStringFromQueryString("wtid", ""); } }
        public string strOperatorInstanceId
        { get { return this.GetStringFromQueryString("OperatorInstanceId", ""); } }
        public string strWorkflowInsId
        { get { return this.GetStringFromQueryString("WorkflowInsId", ""); } }
        public string strView
        { get { return this.GetStringFromQueryString("view", ""); } }
        public string strOrderProduct
        {
            get { return orderProduct; }
        }

        public string strStartDate
        {
            get { return ""; }
        }
        public string strMemember
        {
            get { return ""; }
        }
        public string strExplain
        {
            get { return ""; }
        }
        public string strOperStatus
        {
            get { return this.GetStringFromQueryString("OperStatus", ""); }
        }
        public string strEndDate
        {
            get { return DateTime.Now.ToString("yyyy-MM-dd"); }
        }
        public string strOrderId
        {
            get { return orderId; }
        }
        public string strProductId
        {
            get { return productId; }
        }
        #endregion
    }
}